﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
using Petaframe.Generation.Templates;

namespace Petaframe.Generation
{
    public class BSGenerator : GeneratorBase
    {
        public BSGenerator()
        {

        }

        #region GENERATE BS CLASSES

        public override void Generate()
        {
            if (String.IsNullOrEmpty(this.DataTypeNamespace))
                this.DataTypeNamespace = this.Namespace.Replace("Business", "DataType");

            this.DataProvider = this.IsMSDataProvider == true ? "DataProvider.SqlServer" : "DataProvider.Oracle";

            CreateProjectDirectory();
            
            for (int i = 0; i < classList.Count; i++)
            {
                string path = String.Empty;
                string tableName = classList[i];
                tableName = tableName.Remove(0, (tableName.LastIndexOf('.') + 1));
                this.TableName = tableName;
                this.ClassName = tableName.Replace(" ", "");
                path = this.FilePath + "\\" + this.ClassName + "BS.cs";

                GenerateBSClassCode(path);
            }

            GenerateProjectAndAssemblyFile();
            CreateBussinessBaseClass(this.Namespace);
        }
        private void CreateBussinessBaseClass(string namespacee)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("using System;");
            sb.AppendLine("using System.Data;");
            sb.AppendLine("using System.Diagnostics;");
            sb.AppendLine("using Petaframe.DataAccess;");
            sb.AppendLine("using Petaframe.Business;");
            sb.AppendLine(Environment.NewLine);
            sb.AppendLine(string.Format("namespace {0}", namespacee));
            sb.AppendLine("{");
            string path = String.Empty;
            path = this.FilePath + "\\BusinessBase.cs";

            sb.AppendLine(File.ReadAllText(Environment.CurrentDirectory + "\\BusinessBase.txt"));

            File.WriteAllText(path, sb.ToString());
        }
        private void CreateProjectDirectory()
        {
            if (!Directory.Exists(this.FilePath))
                Directory.CreateDirectory(this.FilePath);
        }

        private void GenerateProjectAndAssemblyFile()
        {
            GenerateBSProjectFile();
            AssemblyInfoDosyasiniOlustur();
        }

        private void GenerateBSClassCode(string path)
        {
            string generationText = String.Empty;

            DataTable dt = TabloBilgileriniAl();

            PetaframeBSClassTextTemplate temp = new PetaframeBSClassTextTemplate();
            temp.PrimaryKeyColumnsAsParameter = this.PrimaryKeyColumnsAsParameter;
            temp.PrimaryKeyColumnName = String.IsNullOrEmpty(this.PrimaryKeyColumnName) ? String.Empty : this.PrimaryKeyColumnName;
            temp.ProviderName = this.DataProvider;
            temp.DataTypeNamespace = this.DataTypeNamespace;
            temp.ClassName = this.ClassName;
            temp.Namespace = this.Namespace;
            temp.TableName = this.TableName;
            generationText = temp.TransformText();

            File.WriteAllText(path, generationText);
        }

        private DataTable TabloBilgileriniAl()
        {
            IDataProvider dataProvider = GetDataProvider();
            DataTable dt = dataProvider.TabloBilgileriniAl();
            this.PrimaryKeyColumnName = dataProvider.PrimaryKeyColumnName;
            this.PrimaryKeyColumnsAsParameter = dataProvider.PrimaryKeyColumnsAsParameter;
            return dt;
        }

        private void GenerateBSProjectFile()
        {
            string projectFileContent = ProjectFileContent.Content;
            string references = ProjectFileContent.BSReferences(this.DataTypeNamespace);
            string classNameList = String.Empty;

            for (int i = 0; i < classList.Count; i++)
            {
                string tableName = classList[i];
                tableName = tableName.Remove(0, (tableName.LastIndexOf('.') + 1));
                classNameList += String.Format("<Compile Include=\"{0}BS.cs\" />" + Environment.NewLine, tableName.Replace(" ", ""));
            }

            projectFileContent = String.Format(projectFileContent, Guid.NewGuid().ToString().ToUpper(), this.Namespace, this.Namespace, references, classNameList);
            File.WriteAllText(this.FilePath + "\\" + this.Namespace + ".csproj", projectFileContent);
        }

        private void AssemblyInfoDosyasiniOlustur()
        {
            string propertyFileContent = AssemblyInfoContent.Content;

            propertyFileContent = String.Format(propertyFileContent, this.Namespace, this.Namespace, Guid.NewGuid().ToString());
            Directory.CreateDirectory(this.FilePath + "\\Properties");
            File.WriteAllText(this.FilePath + "\\Properties\\AssemblyInfo.cs", propertyFileContent);
        }

        #endregion
    }
}
